package com.lixiaoyi.config.security;

import com.lixiaoyi.config.security.filter.CheckTokenFilter;
import com.lixiaoyi.config.security.handle.AnonymousAuthenticationHandler;
import com.lixiaoyi.config.security.handle.CustomerAccessDeniedHandler;
import com.lixiaoyi.config.security.handle.LoginFailureHandler;
import com.lixiaoyi.config.security.handle.LoginSuccessHandler;
import com.lixiaoyi.config.security.service.CustomerUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-07-01
 */

@Configuration
@EnableWebSecurity
//开启权限注解控制
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private CustomerUserDetailsService customerUserDetailsService;

    @Resource
    private LoginSuccessHandler loginSuccessHandler;

    @Resource
    private LoginFailureHandler loginFailureHandler;

    @Resource
    private AnonymousAuthenticationHandler anonymousAuthenticationHandler;

    @Resource
    private CustomerAccessDeniedHandler customerAccessDeniedHandler;

    @Resource
    private CheckTokenFilter checkTokenFilter;


    /**
     * 注入加密处理类
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    /**
     * 配置认证处理器的方法
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // 登陆前进行过滤
        http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);



        // 登陆前进行过滤
        http.formLogin()  // 表单登录
                .loginProcessingUrl("/api/user/login").permitAll() // 登录请求url
                // 设置登陆成功或失败后的跳转地址
                .successHandler(loginSuccessHandler)  // 登陆成功
                .failureHandler(loginFailureHandler)  // 登陆失败
                // 禁用csrf防御机制
                .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)  // 不创建Session
                .and()
                .authorizeRequests() // 设置需要拦截的请求
                .antMatchers("/api/user/login").permitAll() // 放行登录请求
                .anyRequest().authenticated()  // 其它一切请求都要拦截
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(anonymousAuthenticationHandler)  // 匿名无权限访问
                .accessDeniedHandler(customerAccessDeniedHandler)   // 认证用户无权限访问
                .and().cors(); // 开启跨域配置

    }


    /**
     * 配置认证处理器
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(customerUserDetailsService).passwordEncoder(passwordEncoder());

    }
}
